home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / libs / garshnelib / garshnelib.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  13KB  |  538 lines

  1. /*
  2.  *  Copyright (c) 1994 Michael D. Bayne.
  3.  *  All rights reserved.
  4.  *
  5.  *  Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include <graphics/gfxmacros.h>
  11. #include <graphics/videocontrol.h>
  12. #include <intuition/intuitionbase.h>
  13.  
  14. #include <clib/exec_protos.h>
  15. #include <clib/dos_protos.h>
  16. #include <clib/graphics_protos.h>
  17. #include <clib/intuition_protos.h>
  18. #include <clib/asl_protos.h>
  19. #include <clib/mathffp_protos.h>
  20. #include <clib/alib_protos.h>
  21.  
  22. #include <pragmas/exec_pragmas.h>
  23. #include <pragmas/dos_pragmas.h>
  24. #include <pragmas/graphics_pragmas.h>
  25. #include <pragmas/intuition_pragmas.h>
  26. #include <pragmas/asl_pragmas.h>
  27. #include <pragmas/mathffp_pragmas.h>
  28.  
  29. #include "Garshnelib_protos.h"
  30. #include "Garshnelib_rev.h"
  31. #include "//defs.h"
  32.  
  33. STATIC const UBYTE VersTag[] = VERSTAG;
  34.  
  35. VOID SASM ScreenModeRequest( AREG(0) struct Window *Wnd,
  36.                             AREG(1) LONG *Mode, AREG(2) LONG *Depth )
  37. {
  38.     struct AslBase *AslBase;
  39.     
  40.     if( AslBase = ( struct AslBase * )OpenLibrary( AslName, 38L ))
  41.     {
  42.         struct ScreenModeRequester *smRequest;
  43.         
  44.         if( smRequest = AllocAslRequestTags( ASL_ScreenModeRequest, TAG_END ))
  45.         {
  46.             if( Depth )
  47.             {
  48.                 if( AslRequestTags( smRequest, ASLSM_Window, Wnd,
  49.                                    ASLSM_SleepWindow, TRUE,
  50.                                    ASLSM_TitleText, "Screen Mode",
  51.                                    ASLSM_InitialDisplayID, *Mode,
  52.                                    ASLSM_InitialDisplayDepth, *Depth,
  53.                                    ASLSM_DoDepth, TRUE, TAG_END ))
  54.                 {
  55.                     *Mode = smRequest->sm_DisplayID;
  56.                     *Depth = smRequest->sm_DisplayDepth;
  57.                 }
  58.             }
  59.             else
  60.             {
  61.                 if( AslRequestTags( smRequest, ASLSM_Window, Wnd,
  62.                                    ASLSM_SleepWindow, TRUE,
  63.                                    ASLSM_TitleText, "Screen Mode",
  64.                                    ASLSM_InitialDisplayID, *Mode, TAG_END ))
  65.                     *Mode = smRequest->sm_DisplayID;
  66.             }
  67.             FreeAslRequest( smRequest );
  68.         }
  69.         CloseLibrary(( struct Library * )AslBase );
  70.     }
  71. }
  72.  
  73. #define NUMCOLORS 45
  74. LONG spectrum[] = {
  75.     0x0F00, 0x0E10, 0x0D20, 0x0C30, 0x0B40, 0x0A50, 0x0960,    0x0870, 0x0780,
  76.     0x0690, 0x05A0, 0x04B0, 0x03C0, 0x02D0,    0x01E0, 0x00F0, 0x00E1, 0x00D2,
  77.     0x00C3, 0x00B4, 0x00A5,    0x0096, 0x0087, 0x0078, 0x0069, 0x005A, 0x004B,
  78.     0x003C,    0x002D, 0x001E, 0x000F, 0x010E, 0x020D, 0x030C, 0x040B,    0x050A,
  79.     0x0609, 0x0708, 0x0807, 0x0906, 0x0A05, 0x0B04,    0x0C03, 0x0D02, 0x0E01
  80.     };
  81.  
  82. VOID SASM setCopperList( DREG(0) LONG Hei, DREG(1) LONG Col,
  83.                         AREG(0) struct ViewPort *VPort,
  84.                         AREG(1) struct Custom *Custom )
  85. {
  86.     struct Library *GfxBase, *IntuitionBase;
  87.     struct UCopList *uCopList;
  88.  
  89.     IntuitionBase = OpenLibrary( "intuition.library", 39 );
  90.     GfxBase = OpenLibrary( "graphics.library", 39 );
  91.     
  92.     if( IntuitionBase && GfxBase &&
  93.        ( uCopList = AllocVec( sizeof( struct UCopList ), MEMF_CLEAR )))
  94.     {
  95.         struct TagItem uCopTags[] = {{VTAG_USERCLIP_SET,0L},{VTAG_END_CM,0L}};
  96.         LONG i, spc;
  97.  
  98.         spc = Hei/NUMCOLORS;
  99.         CINIT( uCopList, NUMCOLORS );
  100.         for( i = 0; i < NUMCOLORS; ++i )
  101.         {
  102.             CWAIT( uCopList, i * spc, 0 );
  103.             CMOVE( uCopList, Custom->color[Col], spectrum[i%NUMCOLORS] );
  104.         }
  105.         CEND( uCopList );
  106.         Forbid();
  107.         VPort->UCopIns = uCopList;
  108.         Permit();
  109.         VideoControl( VPort->ColorMap, uCopTags );
  110.         RethinkDisplay();
  111.     }
  112.     
  113.     if( GfxBase )
  114.         CloseLibrary( GfxBase );
  115.     
  116.     if( IntuitionBase )
  117.         CloseLibrary( IntuitionBase );
  118. }
  119.  
  120. VOID SASM clearCopperList( AREG(0) struct ViewPort *VPort )
  121. {
  122.     struct Library *GfxBase;
  123.     
  124.     if( GfxBase = OpenLibrary( "graphics.library", 37L ))
  125.     {
  126.         struct TagItem uCopTags[] = {{VTAG_USERCLIP_CLR,0L},{VTAG_END_CM,0L}};
  127.         struct UCopList *uCopList;
  128.  
  129.         VideoControl( VPort->ColorMap, uCopTags );
  130.         Forbid();
  131.         uCopList = VPort->UCopIns;
  132.         VPort->UCopIns = 0L;
  133.         Permit();
  134.         FreeVec( uCopList );
  135.     }
  136. }
  137.  
  138. LONG SASM getTopScreenMode( VOID )
  139. {
  140.     struct IntuitionBase *IntuitionBase;
  141.     struct Library *GfxBase;
  142.     struct Screen *pubScr;
  143.     LONG scrMode;
  144.     BPTR lock;
  145.     
  146.     IntuitionBase =
  147.         ( struct IntuitionBase * )OpenLibrary( "intuition.library", 39L );
  148.     GfxBase = OpenLibrary( "graphics.library", 39L );
  149.     
  150.     if( IntuitionBase && GfxBase )
  151.     {
  152.         lock = LockIBase( 0 );
  153.         pubScr = IntuitionBase->FirstScreen;
  154.         scrMode = GetVPModeID(&( pubScr->ViewPort));
  155.         UnlockIBase( lock );
  156.     }
  157.     
  158.     if( GfxBase )
  159.         CloseLibrary( GfxBase );
  160.     
  161.     if( IntuitionBase )
  162.         CloseLibrary(( struct Library * )IntuitionBase );
  163.     
  164.     return scrMode;
  165. }
  166.  
  167. LONG SASM getTopScreenDepth( VOID )
  168. {
  169.     struct IntuitionBase *IntuitionBase;
  170.     LONG Dep = 0;
  171.     
  172.     if( IntuitionBase =
  173.         ( struct IntuitionBase * )OpenLibrary( "intuition.library", 37L ))
  174.     {
  175.         struct Screen *PubScr;
  176.         struct DrawInfo *dri;
  177.         BPTR Lock;
  178.  
  179.         Lock = LockIBase( 0 );
  180.         PubScr = IntuitionBase->FirstScreen;
  181.         if( dri = GetScreenDrawInfo( PubScr ))
  182.         {
  183.             Dep = dri->dri_Depth;
  184.             FreeScreenDrawInfo( PubScr, dri );
  185.         }
  186.         UnlockIBase( Lock );
  187.     
  188.         CloseLibrary(( struct Library * )IntuitionBase );
  189.     }
  190.     
  191.     return Dep;
  192. }
  193.  
  194. struct Screen *SASM cloneTopScreen( DREG(0) LONG MoreColors,
  195.                                    DREG(1) LONG GetPublic )
  196. {
  197.     LONG sMod, sDep, i, Wid, Hei, offx, offy;
  198.     struct IntuitionBase *IntuitionBase;
  199.     struct Screen *Scr, *nScr = 0L;
  200.     struct Rectangle DispRect;
  201.     struct Library *GfxBase;
  202.     struct DrawInfo *dri;
  203.     UWORD *cols;
  204.     BPTR lock;
  205.     
  206.     IntuitionBase =
  207.         ( struct IntuitionBase * )OpenLibrary( "intuition.library", 39L );
  208.     GfxBase = OpenLibrary( "graphics.library", 39L );
  209.     
  210.     if( IntuitionBase && GfxBase )
  211.     {
  212.         /* Lock IntuitionBase so nothing goes away */
  213.         lock = LockIBase( 0 );
  214.         
  215.         /* Grab the first screen and get its attributes */
  216.         if( GetPublic )
  217.             Scr = LockPubScreen( 0L );
  218.         else
  219.             Scr = IntuitionBase->FirstScreen;
  220.         sMod = GetVPModeID(&( Scr->ViewPort )); /* Screen Mode ID */
  221.         offx = Scr->LeftEdge; 
  222.         offy = Scr->TopEdge;
  223.         Wid = Scr->Width;
  224.         Hei = Scr->Height;
  225.         if( dri = GetScreenDrawInfo( Scr ))
  226.             sDep = MoreColors ? dri->dri_Depth + 1 : dri->dri_Depth;
  227.         if( cols = AllocVec( sizeof( WORD ) * ( 1L << sDep ), MEMF_CLEAR ))
  228.             for( i = 0; i < ( 1L << dri->dri_Depth ); ++i )
  229.                 cols[i] = GetRGB4( Scr->ViewPort.ColorMap, i );
  230.         
  231.         if( GetPublic )
  232.             UnlockPubScreen( 0L, Scr );
  233.         
  234.         UnlockIBase( lock );
  235.         
  236.         QueryOverscan( sMod, &DispRect, OSCAN_TEXT );
  237.         
  238.         Wid = min( Wid, DispRect.MaxX - DispRect.MinX + 1 );
  239.         Hei = min( Hei, DispRect.MaxY - DispRect.MinY + 1 );
  240.         
  241.         if( sMod != INVALID_ID )
  242.         {
  243.             nScr = OpenScreenTags( NULL, SA_DisplayID, sMod, SA_Depth, sDep,
  244.                                   SA_Width, Wid, SA_Height, Hei,
  245.                                   SA_Behind, TRUE, SA_Quiet, TRUE, TAG_DONE );
  246.             if( nScr )
  247.             {
  248.                 BltBitMap( Scr->RastPort.BitMap, offx < 0 ? -offx : 0,
  249.                           offy < 0 ? -offy : 0, nScr->RastPort.BitMap, 0, 0,
  250.                           Wid, Hei, 0x00C0, 0x00FF, NULL );
  251.                 LoadRGB4( &(nScr->ViewPort), cols, 1L << sDep );
  252.                 if( offx > 0 )
  253.                     MoveScreen( nScr, offx, 0 );
  254.                 WaitBlit();
  255.                 ScreenToFront( nScr );
  256.             }
  257.         }
  258.         
  259.         if( cols )
  260.             FreeVec( cols );
  261.         
  262.         if( dri )
  263.             FreeScreenDrawInfo( Scr, dri );
  264.     }
  265.     
  266.     if( GfxBase )
  267.         CloseLibrary( GfxBase );
  268.     
  269.     if( IntuitionBase )
  270.         CloseLibrary(( struct Library * )IntuitionBase );
  271.     
  272.     return nScr;
  273. }
  274.  
  275. ULONG *SASM GetColorTable( AREG(0) struct Screen *Screen )
  276. {
  277.     struct Library *GfxBase;
  278.     ULONG *ColorTable = 0L;
  279.  
  280.     if( GfxBase = OpenLibrary( "graphics.library", 39L ))
  281.     {
  282.         LONG NumCols = ( 1L << Screen->RastPort.BitMap->Depth );
  283.  
  284.         ColorTable = AllocVec( sizeof( LONG ) * ( 3 * NumCols + 2 ),
  285.                               MEMF_CLEAR );
  286.         if( ColorTable )
  287.         {
  288.             GetRGB32( Screen->ViewPort.ColorMap, 0, NumCols, ColorTable + 1 );
  289.             ColorTable[0] = ( NumCols << 16L );
  290.         }
  291.  
  292.         CloseLibrary( GfxBase );
  293.     }
  294.  
  295.     return ColorTable;
  296. }
  297.  
  298. LONG SASM AvgBitsPerGun( DREG(0) LONG ModeID )
  299. {
  300.     struct Library *GfxBase;
  301.     LONG BPG = 8;
  302.  
  303.     if( GfxBase = OpenLibrary( "graphics.library", 37L ))
  304.     {
  305.         struct DisplayInfo Inf;
  306.     
  307.         if( GetDisplayInfoData( 0L, ( UBYTE * )&Inf, sizeof( Inf ), DTAG_DISP,
  308.                                ModeID ))
  309.             BPG = ( Inf.RedBits + Inf.GreenBits + Inf.BlueBits ) / 3;
  310.  
  311.         CloseLibrary( GfxBase );
  312.     }
  313.  
  314.     return BPG;
  315. }
  316.  
  317. VOID SASM FadeAndLoadTable( AREG(0) struct Screen *Screen, DREG(0) LONG BPG,
  318.                            AREG(1) ULONG *ColorTable, DREG(1) LONG SavePlanes )
  319. {
  320.     struct Library *IntuitionBase, *GfxBase;
  321.     
  322.     if( !ColorTable )
  323.         return;
  324.  
  325.     IntuitionBase = OpenLibrary( "intuition.library", 39L );
  326.     GfxBase = OpenLibrary( "graphics.library", 39L );
  327.     
  328.     if( IntuitionBase && GfxBase )
  329.     {
  330.         LONG NumCols = ( 1L << ( Screen->RastPort.BitMap->Depth-SavePlanes ));
  331.         LONG i;
  332.         
  333.         for( i = 0; i < 3 * NumCols; ++i )
  334.         {
  335.             if( ColorTable[i+1] > 1L << ( 32 - BPG ))
  336.                 ColorTable[i+1] -= ( 1L << ( 32 - BPG ));
  337.             else
  338.                 ColorTable[i+1] = 0;
  339.         }
  340.             
  341.         LoadRGB32(&( Screen->ViewPort ), ColorTable );
  342.     }
  343.     
  344.     if( GfxBase )
  345.         CloseLibrary( GfxBase );
  346.     
  347.     if( IntuitionBase )
  348.         CloseLibrary( IntuitionBase );
  349. }
  350.  
  351. struct Window *SASM BlankMousePointer( AREG(0) struct Screen *Scr )
  352. {
  353.     struct Library *IntuitionBase, *GfxBase;
  354.     struct Window *Wnd;
  355.     ULONG oldmodes;
  356.     
  357.     IntuitionBase = OpenLibrary( "intuition.library", 39L );
  358.     GfxBase = OpenLibrary( "graphics.library", 39L );
  359.     
  360.     if( IntuitionBase && GfxBase )
  361.     {
  362.         oldmodes = SetPubScreenModes( 0 );
  363.         if( Wnd = OpenWindowTags( 0L, WA_Activate, TRUE, WA_Left, 0, WA_Top, 0,
  364.                                  WA_Width, 1, WA_Height, 1, WA_Borderless,
  365.                                  TRUE, WA_CustomScreen, Scr, TAG_END ))
  366.         {
  367.             if( Wnd->UserData = AllocVec( 3 * 2 * sizeof( UWORD ),
  368.                                          MEMF_CHIP|MEMF_CLEAR ))
  369.             {
  370.                 /* Set a pointer sprite of size 16*1 (alloc and clear data for
  371.                    sprite def which is 2 control words, 2 words 4-color data
  372.                    for each line and 2 empty words at the end (as suggested in
  373.                    Hardware Reference Manual)). */
  374.                 SetPointer( Wnd, ( UWORD * )Wnd->UserData, 1, 16, 0, 0 );
  375.             }
  376.         }
  377.         SetPubScreenModes( oldmodes );
  378.     }
  379.     
  380.     if( GfxBase )
  381.         CloseLibrary( GfxBase );
  382.     
  383.     if( IntuitionBase )
  384.         CloseLibrary( IntuitionBase );
  385.  
  386.     return Wnd;
  387. }
  388.  
  389. VOID SASM UnblankMousePointer( AREG(0) struct Window *Wnd )
  390. {
  391.     struct Library *IntuitionBase;
  392.     
  393.     if( IntuitionBase = OpenLibrary( "intuition.library", 39L ))
  394.     {
  395.         if( Wnd )
  396.         {
  397.             if( Wnd->UserData )
  398.             {
  399.                 ClearPointer( Wnd );
  400.                 FreeVec( Wnd->UserData );
  401.             }
  402.             CloseWindow( Wnd );
  403.         }
  404.         CloseLibrary( IntuitionBase );
  405.     }
  406. }
  407.  
  408. Triplet *AllocTable( LONG Colors, LONG Dep, LONG Offset )
  409. {
  410.     struct Library *MathBase;
  411.     FLOAT incr, p2, p3, f, h;
  412.     LONG i, Shift, ih;
  413.     Triplet *Table;
  414.     
  415.     MathBase = OpenLibrary( "mathffp.library", 0L );
  416.     Table = AllocVec( 2*( Colors + Offset ) * sizeof( Triplet ), MEMF_CLEAR );
  417.     
  418.     if( MathBase && Table )
  419.     {
  420.         incr = SPDiv( SPFlt( Colors ), SPFlt( 360 ));
  421.         Shift = 32 - Dep;
  422.         
  423.         for( i = Offset, h = SPFlt( 0 ); i < Colors + Offset;
  424.             i++, h = SPAdd( h, incr ))
  425.         {
  426.             ih = SPFix( SPDiv( SPFlt( 60 ), h ));
  427.             f = SPSub( SPFlt( ih ), SPDiv( SPFlt( 60 ), h ));
  428.             p2 = SPSub( f, SPFlt( 1 ));
  429.             p3 = f;
  430.             switch( ih )
  431.             {
  432.             case 0:
  433.                 Table[i].Red = Colors << Shift;
  434.                 Table[i].Green = CastAndShift( p3 );
  435.                 Table[i].Blue = 0;
  436.                 break;
  437.             case 1:
  438.                 Table[i].Red = CastAndShift( p2 );
  439.                 Table[i].Green = Colors << Shift;
  440.                 Table[i].Blue = 0;
  441.                 break;
  442.             case 2:
  443.                 Table[i].Red = 0;
  444.                 Table[i].Green = Colors << Shift;
  445.                 Table[i].Blue = CastAndShift( p3 );
  446.                 break;
  447.             case 3:
  448.                 Table[i].Red = 0;
  449.                 Table[i].Green = CastAndShift( p2 );
  450.                 Table[i].Blue = Colors << Shift;
  451.                 break;
  452.             case 4:
  453.                 Table[i].Red = CastAndShift( p3 );
  454.                 Table[i].Green = 0;
  455.                 Table[i].Blue = Colors << Shift;
  456.                 break;
  457.             case 5:
  458.                 Table[i].Red = Colors << Shift;
  459.                 Table[i].Green = 0;
  460.                 Table[i].Blue = CastAndShift( p2 );
  461.                 break;
  462.             }
  463.         }
  464.         CopyMem(&( Table[Offset] ), &( Table[Colors+Offset] ),
  465.                 Colors * sizeof( Triplet ));
  466.     }
  467.     
  468.     if( MathBase )
  469.         CloseLibrary( MathBase );
  470.     else
  471.     {
  472.         FreeVec( Table );
  473.         Table = 0L;
  474.     }
  475.     
  476.     return Table;
  477. }
  478.  
  479. Triplet *SASM RainbowPalette( AREG(0) struct Screen *Screen,
  480.                              AREG(1) Triplet *Table, DREG(0) LONG Offset,
  481.                              DREG(1) LONG EP )
  482. {
  483.     struct Library *IntuitionBase, *GfxBase;
  484.     static LONG ColorPos = 0L;
  485.     
  486.     if( Screen )
  487.     {
  488.         IntuitionBase = OpenLibrary( "intuition.library", 39L );
  489.         GfxBase = OpenLibrary( "graphics.library", 39L );
  490.         
  491.         if( IntuitionBase && GfxBase )
  492.         {
  493.             LONG Colors = ( 1L << Screen->BitMap.Depth ) - Offset;
  494.             LONG PDep = ( Screen->BitMap.Depth < ( 9 - EP )) ?
  495.                 Screen->BitMap.Depth + EP : 8;
  496.             LONG PColors = ( 1L << PDep ) - Offset;
  497.             
  498.             if( !Table )
  499.             {
  500.                 Table = AllocTable( PColors, PDep, Offset );
  501.                 if( Offset )
  502.                     SetRGB32(&(Screen->ViewPort), 0, 0, 0, 0 );
  503.             }
  504.             
  505.             ColorPos = ( ++ColorPos % PColors );
  506.             
  507.             if( Table )
  508.             {
  509.                 LONG Temp1, Temp2;
  510.                 
  511.                 Temp1 = Table[ColorPos].Blue;
  512.                 Temp2 = Table[ColorPos+Colors+1].Red;
  513.                 
  514.                 Table[ColorPos].Blue = ( Colors << 16L ) + Offset;
  515.                 Table[ColorPos+Colors+1].Red = 0;
  516.                 
  517.                 LoadRGB32(&(Screen->ViewPort), &( Table[ColorPos].Blue ));
  518.                 
  519.                 Table[ColorPos].Blue = Temp1;
  520.                 Table[ColorPos+Colors+1].Red = Temp2;
  521.             }
  522.         }
  523.         
  524.         if( GfxBase )
  525.             CloseLibrary( GfxBase );
  526.         
  527.         if( IntuitionBase )
  528.             CloseLibrary( IntuitionBase );
  529.     }
  530.     else
  531.     {
  532.         if( Table )
  533.             FreeVec( Table );
  534.     }
  535.     
  536.     return Table;
  537. }
  538.